/**
 * 作者：
 * 创建时间：2012-6-30 下午1:52:58
 */
package dragon.model.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

import dragon.core.exception.ExceptionHandler;
import dragon.core.io.FileHandler;
import dragon.core.io.LogicFile;
import dragon.core.util.FilePath;
import dragon.core.util.Handler;
import dragon.core.util.StringUtils;
import dragon.model.base.Model;
import dragon.model.bizObject.BizObject;
import dragon.model.bizObject.BizObjectContainer;
import dragon.model.fieldTemplate.FieldTemplate;
import dragon.model.fieldTemplate.FieldTemplateContainer;
import dragon.model.table.Table;
import dragon.model.table.TableContainer;

/**
 * 实现功能：模型加载和查看
 */
public class ModelHandler extends Handler {

	/**
	 * 重新加载所有模型
	 * 
	 * @throws ExceptionHandler
	 */
	public void reloadAll() throws ExceptionHandler {
		// 加载所有数据模型和业务模型
		FieldTemplateContainer.loadAll();
		TableContainer.loadAll();
		BizObjectContainer.loadAll();

		// 后续处理
		FieldTemplateContainer.afterLoad();
		TableContainer.afterLoad();
		BizObjectContainer.afterLoad();

		output("t");
	}

	/**
	 * 加载指定模型
	 * 
	 * @throws ExceptionHandler
	 */
	public void reload() throws ExceptionHandler {
		String model = getParameter("model");// 模型类型
		String id = getParameter("id");// 模型ID
		if ("bizObject".equalsIgnoreCase(model)) {
			if (id != null && !StringUtils.isEmpty(id)) {
				BizObjectContainer.load(id);
			} else {
				BizObjectContainer.loadAll();
			}
		} else if ("table".equalsIgnoreCase(model)) {
			if (id != null && !StringUtils.isEmpty(id)) {
				TableContainer.load(id);
			} else {
				TableContainer.loadAll();
			}
		} else if ("fieldTemplate".equalsIgnoreCase(model)) {
			if (id != null && !StringUtils.isEmpty(id)) {
				FieldTemplateContainer.load(id);
			} else {
				FieldTemplateContainer.loadAll();
			}
		}
		output("t");
	}

	@SuppressWarnings({ "unchecked", "serial" })
	public void tree() throws ExceptionHandler {

		List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> subMenus = new ArrayList<Map<String, Object>>();

		// -------------------------------------------字段模型------------------------------------------------
		Map<String, Object> menu = new HashMap<String, Object>();
		Map<String, Object> subMenu = new HashMap<String, Object>();
		menu.put("id", "FieldTemplate");
		menu.put("text", "字段模型:" + FieldTemplateContainer.size() + "个");
		menu.put("state", "closed");
		menu.put("attributes", new HashMap<String, String>() {
			{
				put("type", "FieldTemplate");
			}
		});
		FieldTemplate[] fieldTemplates = FieldTemplateContainer.getAll();
		for (FieldTemplate fieldTemplate : fieldTemplates) {
			subMenu = new HashMap<String, Object>();
			subMenu.put("id", fieldTemplate.getCode());
			subMenu.put("text", fieldTemplate.getName());
			subMenu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "FieldTemplate");
				}
			});
			subMenus.add(subMenu);
		}
		fieldTemplates = null;
		menu.put("children", subMenus);
		menus.add(menu);
		// -------------------------------------------数据模型------------------------------------------------
		menu = new HashMap<String, Object>();
		subMenus = new ArrayList<Map<String, Object>>();
		menu.put("id", "Table");
		menu.put("text", "数据模型:" + TableContainer.size() + "个");
		menu.put("state", "closed");
		menu.put("attributes", new HashMap<String, String>() {
			{
				put("type", "Table");
			}
		});
		Table[] tables = TableContainer.getAll();
		for (Table table : tables) {
			subMenu = new HashMap<String, Object>();
			subMenu.put("id", table.getCode());
			subMenu.put("text", table.getName());
			subMenu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "Table");
				}
			});
			subMenus.add(subMenu);
		}
		tables = null;
		menu.put("children", subMenus);
		menus.add(menu);
		// -------------------------------------------业务模型------------------------------------------------
		menu = new HashMap<String, Object>();
		subMenus = new ArrayList<Map<String, Object>>();
		menu.put("id", "BizObject");
		menu.put("text", "业务模型:" + BizObjectContainer.size() + "个");
		menu.put("state", "closed");
		menu.put("attributes", new HashMap<String, String>() {
			{
				put("type", "BizObject");
			}
		});
		BizObject[] bizObjects = BizObjectContainer.getAll();
		for (BizObject bizObject : bizObjects) {
			subMenu = new HashMap<String, Object>();
			subMenu.put("id", bizObject.getCode());
			subMenu.put("text", bizObject.getName());
			subMenu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "BizObject");
				}
			});
			subMenus.add(subMenu);
		}
		bizObjects = null;
		menu.put("children", subMenus);
		menus.add(menu);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", menus);
		String s = jsonObject.toJSONString().substring(10);
		s = s.substring(0, s.length() - 1);
		output(s);
	}

	@SuppressWarnings({ "unchecked", "serial" })
	public void tree2() throws ExceptionHandler {

		String type = getParameter("type");
		String folder = getParameter("folder");
		System.out.println("type=" + type + "\tfolder=" + folder);

		List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
		Map<String, Object> menu = new HashMap<String, Object>();
		if (folder == null || StringUtils.isEmpty(folder)) {
			// -------------------------------------------字段模型------------------------------------------------
			menu.put("id", "FieldTemplate");
			menu.put("text", "字段模型:" + FieldTemplateContainer.size() + "个");
			menu.put("state", "closed");
			menu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "FieldTemplate");
					put("folder", "root");
				}
			});
			menus.add(menu);
			// -------------------------------------------数据模型------------------------------------------------
			menu = new HashMap<String, Object>();
			menu.put("id", "Table");
			menu.put("text", "数据模型:" + TableContainer.size() + "个");
			menu.put("state", "closed");
			menu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "Table");
					put("folder", "root");
				}
			});
			menus.add(menu);
			// -------------------------------------------业务模型------------------------------------------------
			menu = new HashMap<String, Object>();
			menu.put("id", "BizObject");
			menu.put("text", "业务模型:" + BizObjectContainer.size() + "个");
			menu.put("state", "closed");
			menu.put("attributes", new HashMap<String, String>() {
				{
					put("type", "BizObject");
					put("folder", "root");
				}
			});
			menus.add(menu);
		} else {
			String rootPath = null;
			String path = null;
			if ("FieldTemplate".equals(type)) {
				rootPath = FilePath.MODEL_FIELDTEMPLATE;
			} else if ("Table".equals(type)) {
				rootPath = FilePath.MODEL_TABLE;
			} else {
				rootPath = FilePath.MODEL_BIZOBJECT;
			}
			path = rootPath;
			if (!"root".equals(folder)) {
				path = path + folder;
			}
			List<LogicFile> folders = FileHandler.getLogicFiles(path, 2, false);
			Map<String, String> attributes = null;
			for (LogicFile lf : folders) {
				menu = new HashMap<String, Object>();
				menu.put("id", lf.getName());
				menu.put("text", lf.getName());
				menu.put("state", "closed");
				attributes = new HashMap<String, String>();
				attributes.put("type", type);
				attributes.put("folder", lf.getPath().substring(rootPath.length()));
				menu.put("attributes", attributes);

				menus.add(menu);
			}
		}

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", menus);
		String s = jsonObject.toJSONString().substring(10);
		s = s.substring(0, s.length() - 1);
		output(s);
	}

	/**
	 * 模型列表
	 */
	@SuppressWarnings("unchecked")
	public void list() throws ExceptionHandler {

		String type = getParameter("type");
		String folder = getParameter("folder");

		Model[] models = null;
		if ("FieldTemplate".equals(type)) {
			models = FieldTemplateContainer.getAll();
		} else if ("Table".equals(type)) {
			models = TableContainer.getAll();
		} else {
			models = BizObjectContainer.getAll();
		}

		Map<String, Object> result = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (Model model : models) {
			if ("root".equals(folder)) {
				if (!StringUtils.isEmpty(model.getPkg())) {
					continue;
				}
			} else {
				if (!folder.equals(model.getPkg())) {
					continue;
				}
			}
			map = new HashMap<String, Object>();
			map.put("name", model.getName());
			map.put("code", model.getCode());
			map.put("orderNum", model.getOrderNum());
			map.put("description", model.getDescription());
			map.put("issystem", model.getIsSystem());
			map.put("isactive", model.getIsActive());
			list.add(map);
		}
		result.put("rows", list);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", result);
		String s = jsonObject.toJSONString().substring(10);
		s = s.substring(0, s.length() - 1);

		jsonObject = null;
		map = null;
		list = null;
		result = null;

		output(s);

		s = null;
	}

}
